<?php

namespace application\collection\controller;

use application\admin\controller\base as auth;
use ticky\request;

class collection_content extends auth {

    private $status = array('0' => '<span class="red">未采集<span>', '1' => '<span style="color:green">已采集<span>', '2' => '已导入', '3' => '<span class="red">采集失败<span>');

    /**
     * 采集节点列表
     */
    public function index() {
        $ret = db('collection_node')->newpage();
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->display('collection_node_list');
    }

    /**
     * 添加采集节点
     */
    public function add() {
        if (isset($_POST['dosubmit'])) {
            if (!$_POST['urlpage'])
                showmsg('网址配置不能为空！');
            $res = db('collection_node')->insert($_POST);
            if ($res) {
                showmsg(L('operation_success'), '/collection/collection_content/index', 1);
            } else {
                showmsg(L('operation_failure'));
            }
        } else {
            $data['pagesize_start'] = 1;
            $data['pagesize_end'] = 10;
            $data['par_num'] = 1;
            $data['sourcetype'] = 2;
            $data['sourcecharset'] = 'gbk';
            $data['title_rule'] = '<title>[内容]</title>';
            $data['content_html_rule'] = '<div([^>]*)>[|]</div>[|]';
            $data['down_attachment'] = 1;
            $data['coll_order'] = 1;
            $this->assign('item', $data);
            $this->display('collection_node_add');
        }
    }

    /**
     * 编辑采集节点
     */
    public function edit() {
        if (isset($_POST['dosubmit'])) {
            $id = isset($_GET['nodeid']) ? intval($_GET['nodeid']) : 0;
            if (db('collection_node')->update($_POST, array('nodeid' => $id))) {
                showmsg(L('operation_success'), '/collection/collection_content/index', 1);
            } else {
                showmsg(L('operation_failure'));
            }
        } else {
            $id = isset($_GET['nodeid']) ? intval($_GET['nodeid']) : 0;
            $data = db('collection_node')->where(array('nodeid' => $id))->find();
            $this->assign('item', $data);
            $this->display('collection_node_add');
        }
    }

    /**
     * 采集测试
     */
    public function collection_test() {
        $id = isset($_GET['nodeid']) ? intval($_GET['nodeid']) : 0;
        if (!$id) {
            showmsg(L('lose_parameters'));
        }
        $data = db('collection_node')->where(array('nodeid' => $id))->find();
        if ($data['urlpage'] == '') {
            showmsg('网址配置不能为空！', 'stop');
        }
        if ($data['sourcetype'] == 1) {
            $url = str_replace('(*)', $data['pagesize_start'], $data['urlpage']);
        } else {
            $url = $data['urlpage'];
        }
        $url_start = $data['url_start'];
        $url_end = $data['url_end'];
        if ($url_start == '' || $url_end == '') {
            showmsg('列表区域配置不能为空！', 'stop');
        }
        $content = \ticky\collection::get_content($url);
        $content = \ticky\collection::get_sub_content($content, $url_start, $url_end);
        if ($content) {
            $content = \ticky\collection::get_all_url($content, $data['url_contain'], $data['url_except']);
            //获取第一篇文章地址来测试
            $articleurl = isset($content['url'][0]) ? $content['url'][0] : '';
            if (!empty($articleurl)) {
                $article = \ticky\collection::get_content($articleurl);
                $article = \ticky\collection::get_filter_html($article, $this->get_config($data));
            } else {
                $article = '内容规则错误！';
            }
        } else {
            $article = '列表规则错误！';
        }
        $this->assign('data', $data);
        $this->assign('content', $content);
        $this->assign('article', $article);
        $this->display('collection_test');
    }

    /**
     * 采集网址
     */
    public function collection_list_url() {
        $id = isset($_GET['nodeid']) ? intval($_GET['nodeid']) : 0;
        if (!$id)
            showmsg(L('lose_parameters'));
        $data = db('collection_node')->where(array('nodeid' => $id))->find();
        if (!$data || $data['urlpage'] == '')
            showmsg('网址配置不能为空！', 'stop');
        if ($data['sourcetype'] == 1) {
            $url = array();
            for ($i = $data['pagesize_start']; $i <= $data['pagesize_end']; $i = $i + $data['par_num']) {
                $url[] = str_replace('(*)', $i, $data['urlpage']);
            }
        } else {
            $url[0] = $data['urlpage'];
        }
        $url_start = $data['url_start'];
        $url_end = $data['url_end'];
        if ($url_start == '' || $url_end == '')
            showmsg('列表区域配置不能为空！', 'stop');
        $i = $j = 0;
        foreach ($url as $v) {
            $content = \ticky\collection::get_content($v);
            $content = \ticky\collection::get_sub_content($content, $url_start, $url_end);
            if (!$content)
                continue;
            $content = \ticky\collection::get_all_url($content, $data['url_contain'], $data['url_except']);
            if (!empty($content['url']))
                foreach ($content['url'] as $k => $v) {
                    $r = db('collection_content')->field('url')->where(array('url' => $v))->find();
                    if (!$r) {
                        db('collection_content')->insert(array('nodeid' => $data['nodeid'], 'status' => 0, 'url' => $v, 'title' => $content['title'][$k]));
                        $j++;
                    } else {
                        $i++;
                    }
                }
        }
        showmsg('操作成功，共去除' . $i . '条重复数据，新增' . $j . '条数据！');
    }

    /**
     * 采集内容
     */
    public function collection_article_content() {
        $id = isset($_GET['nodeid']) ? intval($_GET['nodeid']) : 0;
        if (!$id)
            showmsg(L('lose_parameters'), 'stop');
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $total = isset($_GET['total']) ? intval($_GET['total']) : 0;

        if (!$total)
            $total = db('collection_content')->field('id, url')->where(array('nodeid' => $id, 'status' => 0))->total();
        $total_page = ceil($total / 2);

        $list = db('collection_content')->field('id, url')->where(array('nodeid' => $id, 'status' => 0))->order('id DESC')->limit('2')->select();

        if (empty($list))
            showmsg('没有找到网址列表，请先进行网址采集！', 'stop');
        $data = db('collection_node')->field('sourcecharset,down_attachment,watermark,title_rule,title_html_rule,time_rule,time_html_rule,content_rule,content_html_rule')->where(array('nodeid' => $id))->find();
        $i = 0;
        foreach ($list as $v) {
            $article = \ticky\collection::get_content($v['url']);
            //debug($article);
            $article = \ticky\collection::get_filter_html($article, $this->get_config($data));

            if ($data['down_attachment']) {
                $article['content'] = grab_image($article['content'], \ticky\collection::$url);
            }
            if ($article['content']) {
                db('collection_content')->update(array('status' => 1, 'data' => array2string($article)), array('id' => $v['id']));
            } else {
                db('collection_content')->update(array('status' => 3, 'data' => ""), array('id' => $v['id']));
            }
            $i++;
        }
        if ($total_page > $page) {
            showmsg('采集正在进行中，采集进度:' . ($i + ($page - 1) * 2) . '/' . $total, url('collection_article_content', array('nodeid' => $id, 'page' => $page + 1, 'total' => $total)), 5);
        } else {
            db('collection_node')->update(array('lastdate' => TIMESTAMP), array('nodeid' => $id));
            showmsg('采集完成！', '/collection/collection_content/collection_list', 2);
        }
    }

    /**
     * 采集列表
     */
    public function collection_list() {
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        $nodeid = isset($_GET['nodeid']) ? intval($_GET['nodeid']) : 0;
        $where = '1 = 1';
        if (isset($_GET['dosubmit'])) {
            $type = isset($_GET['type']) ? intval($_GET['type']) : 0;
            $status = isset($_GET['status']) ? intval($_GET['status']) : 99;
            $keyword = isset($_GET['keyword']) ? safe_replace($_GET['keyword']) : '';
            if ($type && $keyword) {
                if ($type == '1')
                    $where .= ' AND nodeid = \'' . $keyword . '\'';
                else
                    $where .= ' AND title LIKE \'%' . $keyword . '%\'';
            }
            if ($status != 99) {
                $where .= ' AND status = ' . $status;
            }
        }
        $ret = db('collection_content')->where($where)->newpage();

        foreach ($ret['items'] as $row) {
            $row['level'] = $this->status[$row['status']];
            $data[] = $row;
        }
        $this->assign('keyword', $keyword);
        $this->assign('status', $status);
        $this->assign('type', $type);

        $this->assign('page', $ret['page']);
        $this->assign('items', $data);
        $this->display('collection_list');
    }

    /**
     * 删除采集列表
     */
    public function collection_list_del() {
        if ($_POST && is_array($_POST['ids'])) {
            if (db('collection_content')->delete($_POST['ids'], true)) {
                showmsg(L('operation_success'), '', 1);
            } else {
                showmsg(L('operation_failure'));
            }
        }
    }

    /**
     * 内容导入
     */
    public function collection_content_import() {
        $ids = request::post('ids', []);
        if (count($ids) > 0) {
            $ids = join(',', $ids);
        } else {
            showmsg(L('lose_parameters'), 'stop');
        }
        $this->assign('ids', $ids);
        $this->display('collection_content_import');
    }

    /**
     * 新建内容发布方案
     */
    public function create_programme() {
        if (!isset($_POST['dosubmit']))
            showmsg(L('lose_parameters'), 'stop');

        $ids = safe_replace($_GET['ids']);
        $ids_arr = explode(',', $ids);
        $ids_arr = array_map('intval', $ids_arr);
        $ids = join(',', $ids_arr);

        $modelid = get_category($_GET['catid'], 'modelid');
        if (!$modelid) {
            showmsg(L('illegal_operation'), 'stop');
        }
        $tablename = get_model($modelid);

//tky_content
        $arr = array();
        $arr['modelid'] = $modelid;
        $arr['catid'] = request::post('catid', '0');
        $arr['siteid'] = $this->siteid;
        $arr['uid'] = $this->uid;
        $arr['title'] = '';
        $arr['description'] = '';
        $arr['url'] = '';
        $arr['flag'] = '';
        $arr['tag'] = '';
        $arr['sort'] = 0;
        $arr['allowcomment'] = 0;
        $arr['views'] = 0;
        $arr['replies'] = 0;
        $arr['readperm'] = 0;
        $arr['readpoint'] = 0;
        $arr['is_push'] = 0;
        $arr['status'] = 1;
        $arr['inputtime'] = TIMESTAMP;
        $arr['updatetime'] = TIMESTAMP;
        $arr['author'] = request::post('nickname', '管理员');
        $arr['befrom'] = request::post('copyfrom', '原创');
        $i = 0;
        $res = db('collection_content')->field('id AS cid,status,data')->where('id IN(' . $ids . ')')->order('id DESC')->select();
        foreach ($res as $v) {
            if ($v['status'] != 1)
                continue;
            $arr = array_merge($arr, string2array($v['data']));
            if (!$arr['content'])
                continue;
            $arr['content'] = closetags($arr['content']);
//自动提取缩略图
            if ($_POST['auto_thumb']) {
                $img = match_img($data['content']);
                $data['thumb'] = $img ? thumb($img) : '';
            }
//自动提取内容摘要
            if ($_POST['auto_description']) {
                $arr['description'] = cutstr(trim(strip_tags($arr['content'])), 200);
            }

            $contentid = db('content')->insert($arr);
            db('content')->update(array('url' => $this->get_url($arr['catid'], $contentid)), array('contentid' => $contentid));
            $arr['contentid'] = $contentid;
            db($tablename)->insert($arr, false, false);

            db('collection_content')->update(array('status' => 2), array('id' => $v['cid']));
            $i++;
        }

        showmsg('导入完成，共导入' . $i . '篇内容！', url('collection_list'));
    }

    /**
     * 获取配置信息
     */
    private function get_config($data) {
        return array(
            'title_rule' => \ticky\collection::myexp('[内容]', $data['title_rule']),
            'title_html_rule' => \ticky\collection::myexp('[|]', $data['title_html_rule']),
            'time_rule' => \ticky\collection::myexp('[内容]', $data['time_rule']),
            'time_html_rule' => \ticky\collection::myexp('[|]', $data['time_html_rule']),
            'content_rule' => \ticky\collection::myexp('[内容]', $data['content_rule']),
            'content_html_rule' => \ticky\collection::myexp('[|]', $data['content_html_rule']),
        );
    }

}
